//
//  KYProvinceViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/11.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYProvinceViewController.h"
#import "KYPlaceModel.h"
#import "KYCitiesViewController.h"
static NSString *provinceCellID = @"provinceCellID";
@interface KYProvinceViewController ()
@property (nonatomic,strong)NSArray *placeModel;
@end

@implementation KYProvinceViewController
-(NSArray *)placeModel{
    if (!_placeModel) {
        _placeModel = [KYPlaceModel getPlaceModel];
    }
    return _placeModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:provinceCellID];
}


#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.placeModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:provinceCellID forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:provinceCellID];
    }
    if ([self.placeModel[indexPath.row] cities]) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.textLabel.text = [self.placeModel[indexPath.row] name];
    return cell;
}
#pragma mark - 代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果有城市就跳转城市页面,没有就发通知回主控制器
    if ([self.placeModel[indexPath.row] cities]) {
        KYCitiesViewController *citiesVC = [[KYCitiesViewController alloc]init];
        citiesVC.cities = [self.placeModel[indexPath.row]cities];
        [self.navigationController pushViewController:citiesVC animated:YES];
    }else{
        KYPlaceModel *model = self.placeModel[indexPath.row];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"placeNotification" object:nil userInfo:@{@"place":model.name}];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
